<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="关键字">
              <a-input v-model="queryParam.keyword"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="楼栋">
               <a-select
              v-model="queryParam.building"
                  placeholder="请选择楼栋">
                <a-select-option v-for="(build,cindex) in buildingOptions"  :value="build.value" :key="cindex">{{build.name}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col> 
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="onSearch">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              <a @click="toggleAdvanced" style="margin-left: 8px" v-if="false">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="handleAdd" >新建</a-button>
      <a-dropdown v-if="  selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1"><a-icon type="delete" />删除</a-menu-item>
          <!-- lock | unlock -->
          <a-menu-item key="2"><a-icon type="lock" />锁定</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="{ show: true, clear: () => { this.selectedRowKeys = [] } }"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      @change="handleChange"
    >
      <span slot="action" slot-scope="text, record">
        <template >
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
        </template>
        <a-dropdown>
          <a class="ant-dropdown-link">
            更多 <a-icon type="down" />
          </a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a href="javascript:;">详情</a>
            </a-menu-item>
            <a-menu-item >
              <a href="javascript:;">禁用</a>
            </a-menu-item>
            <a-menu-item>
              <a-popconfirm title="确定删除么？" @confirm="delConfirm(record)"  okText="Yes" cancelText="No">
    <a href="javascript:;">删除</a>
  </a-popconfirm>
            </a-menu-item>
          
          </a-menu>
        </a-dropdown>
      </span>
    </s-table>

    <a-modal
      title="操作"
      :width="800"
      v-model="visible"
      @ok="handleOk"
    >
      <a-form  :form="form">

        <a-form-item
           v-show="false"  
        >
          <a-input  style="width: 100%" v-decorator="[
          'id',
          {rules: [ ]}
        ]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="楼栋"
          hasFeedback 
        >
         <a-select  
             v-decorator="[
          'building',
          {rules: [{ required: true, message: '请输入楼栋' }]}
        ]"
        placeholder="请输入楼栋"
          >
             <a-select-option v-for="(build,cindex) in buildingOptions"  :value="build.value" :key="cindex">{{build.name}}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="房号"
          hasFeedback 
        >
          <a-input style="width: 100%"  v-decorator="[
          'code',
          {rules: [{ required: true, message: '请输入房号' }]}
        ]"
        placeholder="请输入房号"/>
        </a-form-item>


        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="教室名称"
          hasFeedback 
        >
          <a-input style="width: 100%"  v-decorator="[
          'name',
          {rules: [{ required: true, message: '请输入教室名称' }]}
        ]"
        placeholder="请输入教室名称"/>
        </a-form-item>

                <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="教室类型"
        >
          <a-select  
              v-decorator="[
              'type',
              {rules: [{ required: true, message: '请输入教室类型' }]}
              ]"
               placeholder="请输入教室类型"
          >
            <a-select-option :value="0">普通</a-select-option>
            <a-select-option :value="1">多媒体</a-select-option>
          </a-select>
        </a-form-item>

          <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="座位数"
          hasFeedback 
        > 

          <a-input style="width: 100%"  v-decorator="[
          'seatCount',
          {rules: [{ required: true, message: '请输入座位数' }]}
        ]"
        placeholder="请输入座位数"/>
        </a-form-item>
  

        <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="宣言"
          hasFeedback
          help="请填写一段宣言"
        >
          <a-textarea :rows="5" v-model="mdl.sign" placeholder="..." id="description"/>
        </a-form-item> -->


      </a-form>
    </a-modal>


  </a-card>
</template>

<script>
import STable from '@/components/table/'
import ATextarea from 'ant-design-vue/es/input/TextArea'
import AInput from 'ant-design-vue/es/input/Input'
import moment from 'moment'

import { getRoleList, getRoomList, addRoom, editRoom, getRoom, delRoom, getBuildingList} from '@/api/manage'

export default {
  name: 'RoomList',
  components: {
    AInput,
    ATextarea,
    STable
  },
  data() {
    return {
      visible: false,
      //        description: '全校班级管理。',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 12 }
      },
      form: this.$form.createForm(this),
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '名称',
          dataIndex: 'name'
        },
        {
          title: '楼栋',
          dataIndex: 'building',
          customRender: text => {
            switch (text) {
              case 1:
                return '文德楼'
              case 2:
                return '明德楼'
            }
          }
        },
        {
          title: '房间号',
          dataIndex: 'code'
        },
        {
          title: '座位数',
          dataIndex: 'seatCount',
          needTotal: true,
          sorter: (a, b) => a.seatCount - b.seatCount
        },
        {
          title: '类型',
          dataIndex: 'type',
          customRender: text => {
            switch (text) {
              case 0:
                return '普通'
              case 1:
                return '多媒体'
            }
          },
          filters: [
            {
              text: '普通',
              value: '0'
            },
            {
              text: '多媒体',
              value: '1'
            }
          ],
          // filterMultiple: false
        },
        {
          table: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        let that = this
        console.log('loadData parameter'); 
        if(typeof parameter.type !='undefined' && parameter.type.length){ 
          parameter.type = JSON.stringify(parameter.type);
        } 
        console.log(parameter); 
        return getRoomList(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.result
            console.log(res.result)
          })
          .catch(err => {
            that.$notification['error']({
              message: '错误',
              description: ((err.response || {}).data || {}).message || '请求出现错误，请稍后再试',
              duration: 4
            })
          })
      },
      selectedRowKeys: [],
      selectedRows: [],
      buildingOptions: []
    }
  },
  created() {
    getRoleList({ t: new Date() })
     let self = this
     getBuildingList().then(res => {
        self.buildingOptions = res.result.list
      }).catch(err =>{
        //抓异常
        console.log(err);
     })
  },
  methods: {
    // 增
    handleAdd() {
      this.visible = true
      this.form.resetFields()
    },
    // 改
    handleEdit(record) {
      let self = this
      getRoom({ id: record.id }).then(res => {
        self.form.setFieldsValue({ ...res.result })
      })
      // console.log(this.mdl)
      this.visible = true
    },
    // 增改 处理
    handleOk(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          if (values.id) {
            let self = this
            editRoom(values).then(res => {
              self.$message.success('修改成功')
              self.$refs.table.refresh(true)
              self.visible = false
            })
          } else {
            let self = this
            addRoom(values).then(res => {
              self.$message.success('添加成功')
              self.$refs.table.refresh(true)
              self.visible = false
            })
          }
        }
      })
    },
    // 删
    delConfirm(record) {
      console.log(record)
      let self = this
      delRoom({ ids: record.id }).then(res => {
        console.log(res)
        self.$message.success('删除成功')
        self.$refs.table.refresh(true)
      })
    },
    // 查
    onSearch() {
      console.log(this.$refs.table.localPagination)
      console.log(this.queryParam)
      let self = this
      getRoomList(this.queryParam).then(res => {
        self.$refs.table.refresh(true)
      })
    },
    handleChange(filters) {
      console.log(filters)
    },
    onFilterDropdownVisibleChange(visible) {
      console.log(visible)
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      console.log(this.electedRowKeys, this.selectedRows)
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },

    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  },
  watch: {
    // 'selectedRows': function (selectedRows) {
    //   this.needTotalList = this.needTotalList.map(item => {
    //     return {
    //       ...item,
    //       total: selectedRows.reduce( (sum, val) => {
    //         return sum + val[item.dataIndex]
    //       }, 0)
    //     }
    //   })
    // }
  }
}
</script>